package com.home.duy.GUI;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JInternalFrame;

import com.home.Common.Resources_Duy;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.home.Validation.Validator;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.KhuVucBanDAO;
import com.home.duy.code.DAO.NhanVienDAO;
import com.home.duy.code.FileFilter.ImageFileFilter;
import com.home.duy.code.Model.QLBanTableModel;
import com.home.duy.code.components.ImageFileView;
import com.home.duy.code.components.ImagePanel;

import javax.swing.JButton;
import com.home.Common.Common;
import com.home.Entity.Ban;
import com.home.Entity.KhuVucBan;
import com.home.Entity.NhanVien;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

public class QuanLiBan extends JInternalFrame {

	private static final int MAX_NUMBER_PER_PAGE = 10;
	private static final int MAX_LENGHT_TEN_BAN = 50;
	private static final int MAX_LENGHT_MOTA = 100;
	private QLBanTableModel pagingModel = null;
	private TableRowSorter<QLBanTableModel> sorter = null;
	private int indexPos = 0;
	private BufferedImage imgCurrent = null; // @jve:decl-index=0:
	private String extension = ""; // @jve:decl-index=0:

	private JPanel jContentPane = null;
	private JPanel panelDuLieu = null;
	private JScrollPane scrollPaneTable = null;
	private JTable tableData = null;
	private JPanel panelCommand = null;
	private JButton btnThem = null;
	private JButton btnSua = null;
	private JButton btnXoa = null;
	private JTextField txtFilter = null;
	private JLabel jLabel7 = null;
	private JPanel panelChiTiet = null;
	private ImagePanel panelComponent = null;
	private JPanel panelImg = null;
	private JLabel jLabel = null;
	private JPanel jPanel = null;
	private JButton btnChange = null;
	private JLabel imgBan = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField txtTenBan = null;
	private JComboBox comboBoxKhuVuc = null;
	private JLabel jLabel3 = null;
	private JComboBox comboBoxNhanVien = null;
	private JLabel jLabel4 = null;
	private JRadioButton rbDuoc = null;
	private JRadioButton rbKhong = null;
	private ButtonGroup groupSudung = new ButtonGroup();
	private JLabel jLabel5 = null;
	private JScrollPane scrollPaneMota = null;
	private JTextArea txtMoTa = null;
	private JButton btnChapNhan = null;
	private JButton btnLamLai = null;
	private JButton btnNext = null;
	private JButton btnPrevious = null;
	private JButton btnFirst = null;
	private JButton btnLast = null;
	private JLabel lblPage = null;
	private JPanel panelPaging = null;

	/**
	 * This is the xxx default constructor
	 */
	public QuanLiBan() {
		super();
		initialize();
		Ban b = getFirstRecordBan();
		if (b != null)
			fillData(b);
		else {
			resetAllField();

		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(800, 600);
		this.setContentPane(getJContentPane());
		this.setTitle("Quản Lý Thông Tin " + Resources_Duy.TITLE_MENU_ITEM_BAN);
		this.setClosable(true);
		this.setIconifiable(true);
		// this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	// ====================== PROCESS METHOD ========================

	private int getSizeData() {
		int size = 0;
		try {
			BanDAO banDao = new BanDAO();
			size = banDao.countSizeData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return size;
	}

	private int getNumberRowOdd() {
		return getSizeData() % MAX_NUMBER_PER_PAGE;
	}

	private int getNumberPage() {
		return getSizeData() / MAX_NUMBER_PER_PAGE;
	}

	private Ban getBanSelected() {
		Ban ban = null;
		try {
			if (getSizeData() > 0) {
				int index = getTableData().getSelectedRow();
				if (index < 0) {
					JOptionPane.showMessageDialog(this, "Bạn Hãy Chọn Bàn !",
							"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
					return null;
				}
				BanDAO banDao = new BanDAO();
				ban = (Ban) banDao.findByPk(Ban.class, (Long
						.parseLong((getTableData().getModel().getValueAt(index,
								5)).toString())));
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
		return ban;
	}

	private void processSua() {
		try {
			if (getBanSelected() != null)
				fillData(getBanSelected());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void processThem() {
		resetAllField();
	}

	private Ban getFirstRecordBan() {
		Ban ban = null;
		try {
			BanDAO banDao = new BanDAO();
			ban = (Ban) banDao.findByFirstRecord();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
		return ban;
	}

	private void fillData(Ban ban) {
		txtTenBan.setText(ban.getTen());
		comboBoxKhuVuc.getModel().setSelectedItem(ban.getKhuVucBan());
		comboBoxNhanVien.getModel().setSelectedItem(ban.getNhanVien());
		if (ban.isSuDung())
			rbDuoc.setSelected(true);
		else
			rbKhong.setSelected(true);
		txtMoTa.setText(ban.getMoTa());
		ImageIcon img = null;
		try {
			File f = new File(Resources_Duy.PATH_SAVE_IMAGE + ban.getHinhAnh());
			if (f.isFile()) {
				BufferedImage im = Common.getBufferImage(f);

				img = Common.convertBufferImageToImageIcon(im);
			} else
				img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_BAN);
		} catch (NullPointerException e) {
			img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_BAN);
		}
		imgBan.setIcon(img);
	}

	private Vector<NhanVien> getAllNhanVien() {
		Vector<NhanVien> nvs = new Vector<NhanVien>();
		try {
			NhanVienDAO nvDao = new NhanVienDAO();
			List<NhanVien> list = nvDao.findAll(NhanVien.class);
			for (NhanVien nv : list) {
				nvs.add(nv);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

		return nvs;
	}

	private Vector<KhuVucBan> getAllKVB() {
		Vector<KhuVucBan> kvbs = new Vector<KhuVucBan>();
		try {
			KhuVucBanDAO kvbDao = new KhuVucBanDAO();
			List<KhuVucBan> list = kvbDao.findAll(KhuVucBan.class);
			for (KhuVucBan kvb : list) {
				kvbs.add(kvb);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
		return kvbs;
	}

	private void saveObject() {
		try {
			if (validation()) {
				Ban ban = null;
				String thongBao = "";
				int index = getTableData().getSelectedRow();
				Ban banSelected = null;
				if (index > -1)
					banSelected = getBanSelected();
				if (banSelected != null) {
					ban = banSelected;
					thongBao = "Cập Nhật Thành Công !";
				} else {
					ban = new Ban();
					ban.setMaSo(Common.getID());
					thongBao = "Thêm Mới Thành Công !";
				}
				ban.setTen(txtTenBan.getText());
				ban.setKhuVucBan((KhuVucBan) comboBoxKhuVuc.getModel()
						.getSelectedItem());
				ban.setNhanVien((NhanVien) comboBoxNhanVien.getModel()
						.getSelectedItem());
				if (rbDuoc.isSelected())
					ban.setSuDung(true);
				else
					ban.setSuDung(false);
				ban.setMoTa(txtMoTa.getText().trim());
				if (imgCurrent != null)
					ban.setHinhAnh(ban.getMaSo() + extension);

				BanDAO banDao = new BanDAO();
				if (banDao.saveOrUpdate(ban)) {
					if (imgCurrent != null) {
						File f = new File(Resources_Duy.PATH_SAVE_IMAGE
								+ ban.getHinhAnh());
						if (f.isFile()) {
							f.delete();
						}
						Common.writeImgToFile(
								imgCurrent,
								extension.substring(1),
								Resources_Duy.PATH_SAVE_IMAGE
										+ ban.getHinhAnh());
					}
					JOptionPane.showMessageDialog(this, thongBao, "Thông Báo",
							JOptionPane.INFORMATION_MESSAGE);

					if (banSelected == null) {
						indexPos = (getSizeData() / MAX_NUMBER_PER_PAGE)
								* MAX_NUMBER_PER_PAGE;
						if (getNumberRowOdd() > 0) {
							pagingModel.setIndexRow(indexPos);
							pagingModel.setRowCount(getNumberRowOdd());
						} else {
							indexPos = indexPos - MAX_NUMBER_PER_PAGE;
							pagingModel.setIndexRow(indexPos);
							pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
						}

						btnNext.setEnabled(false);
						btnLast.setEnabled(false);
						btnPrevious.setEnabled(true);
						btnFirst.setEnabled(true);

						lblPage.setText(getSizeData() + " / " + getSizeData());

					}

					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();
				} else {
					JOptionPane.showMessageDialog(this, "Có Lỗi Xảy Ra !",
							"Lỗi", JOptionPane.ERROR_MESSAGE);
				}

				if (getSizeData() <= MAX_NUMBER_PER_PAGE) {
					btnPrevious.setEnabled(false);
					btnFirst.setEnabled(false);
				}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void processDelete() {
		try {
			BanDAO banDao = new BanDAO();
			if (JOptionPane.showConfirmDialog(this,
					"Bạn Có Thực Sự Muốn Xóa Bàn Này Không ?", "Thông Báo",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
				Ban banSelected = getBanSelected();
				if (banSelected != null)
					if (banDao.delete(banSelected)) {
						File f = new File(Resources_Duy.PATH_SAVE_IMAGE
								+ banSelected.getHinhAnh());
						if (f.isFile())
							f.delete();
						JOptionPane.showMessageDialog(this,
								"Đã Xóa Thành Công Bàn "
										+ banSelected.getTen().toUpperCase(),
								"Thông Báo", JOptionPane.INFORMATION_MESSAGE);

						if (getSizeData() > MAX_NUMBER_PER_PAGE) {
							if (getNumberRowOdd() >= 0
									&& indexPos + MAX_NUMBER_PER_PAGE > getSizeData()) {
								pagingModel.setIndexRow(indexPos);
								pagingModel.setRowCount(getNumberRowOdd());
								lblPage.setText(getSizeData() + " / "
										+ getSizeData());
							} else
								lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE
										+ " / " + getSizeData());
							((AbstractTableModel) getTableData().getModel())
									.fireTableDataChanged();
						} else {
							indexPos = 0;
							pagingModel.setIndexRow(indexPos);
							pagingModel.setRowCount(getSizeData());
							lblPage.setText(getSizeData() + " / "
									+ getSizeData());
							if (getSizeData() > 0)
								((AbstractTableModel) getTableData().getModel())
										.fireTableDataChanged();
						}
					}

				if (getSizeData() <= MAX_NUMBER_PER_PAGE) {
					btnPrevious.setEnabled(false);
					btnFirst.setEnabled(false);
				}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Có Lỗi Xảy Ra !", "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void resetAllField() {
		txtTenBan.setText("");
		txtTenBan.requestFocus();
		comboBoxKhuVuc.setSelectedItem(null);
		comboBoxNhanVien.setSelectedItem(null);
		rbDuoc.setSelected(true);
		txtMoTa.setText("");
		imgBan.setIcon(Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT));
		imgCurrent = null;
		extension = "";
	}

	private void processChangeImage() {
		try {
			Common.tranformJFileChooserToVi();
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.addChoosableFileFilter(new ImageFileFilter());
			fileChooser.setFileView(new ImageFileView());

			int returnVal = fileChooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File fileImg = fileChooser.getSelectedFile();

				BufferedImage img = Common.getBufferImage(fileImg);

				if (img.getWidth() > 128 || img.getHeight() > 128) {
					JOptionPane.showMessageDialog(this,
							"Kích Thước Hình Ảnh Không Phù Hợp !", "Thông Báo",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}

				ImageIcon ic = Common.convertBufferImageToImageIcon(img);
				imgBan.setIcon(ic);
				imgCurrent = img;
				extension = Common.getExtension(fileImg.getName());
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private boolean validation() {
		boolean check = true;
		if (Validator.isBlank(txtTenBan.getText())) {
			check = false;
			JOptionPane.showMessageDialog(this, "Bạn Hãy Nhập Tên Bàn !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			txtTenBan.requestFocus();
		} else if (txtTenBan.getText().trim().length() > MAX_LENGHT_TEN_BAN) {
			check = false;
			JOptionPane.showMessageDialog(this,
					"Tên Bàn Đã Vượt Quá Giới Hạn !", "Thông Báo",
					JOptionPane.INFORMATION_MESSAGE);
			txtTenBan.requestFocus();
		} else if (comboBoxKhuVuc.getSelectedItem() == null) {
			check = false;
			JOptionPane.showMessageDialog(this,
					"Bạn Hãy Chọn Khu Vực Của Bàn !", "Thông Báo",
					JOptionPane.INFORMATION_MESSAGE);
			comboBoxKhuVuc.requestFocus();
		} else if (comboBoxNhanVien.getSelectedItem() == null) {
			check = false;
			JOptionPane.showMessageDialog(this, "Bạn Hãy Chọn Nhân Viên QL !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			comboBoxNhanVien.requestFocus();
		} else if (txtMoTa.getText().trim().length() > MAX_LENGHT_MOTA) {
			check = false;
			JOptionPane.showMessageDialog(this, "Mô Tả Đã Vượt Quá Giới Hạn !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			txtMoTa.requestFocus();
		}
		return check;
	}

	private void locDuLieu() {
		if (Validator.isNotBlank(txtFilter.getText()))
			sorter.setRowFilter(RowFilter.regexFilter(txtFilter.getText()));
		else
			sorter.setRowFilter(null);
	}

	// ====================== END PROCESS METHOD ====================

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelDuLieu(), null);
			jContentPane.add(getPanelChiTiet(), null);
			jContentPane.add(getBtnChapNhan(), null);
			jContentPane.add(getBtnLamLai(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes panelDuLieu
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelDuLieu() {
		if (panelDuLieu == null) {
			lblPage = new JLabel();
			lblPage.setText("");
			lblPage.setHorizontalAlignment(SwingConstants.CENTER);
			lblPage.setBounds(new Rectangle(110, 1, 55, 20));
			panelDuLieu = new JPanel();
			panelDuLieu.setLayout(null);
			panelDuLieu.setBounds(new Rectangle(12, 16, 767, 277));
			panelDuLieu.setBorder(BorderFactory
					.createTitledBorder("Danh Sách Bàn"));
			panelDuLieu.add(getScrollPaneTable(), null);
			panelDuLieu.add(getPanelCommand(), null);
			panelDuLieu.add(getPanelPaging(), null);
		}
		return panelDuLieu;
	}

	/**
	 * This method initializes scrollPaneTable
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneTable() {
		if (scrollPaneTable == null) {
			scrollPaneTable = new JScrollPane();
			scrollPaneTable.setBounds(new Rectangle(15, 51, 738, 188));
			scrollPaneTable.setViewportView(getTableData());
		}
		return scrollPaneTable;
	}

	/**
	 * This method initializes tableData
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTableData() {
		if (tableData == null) {
			tableData = new JTable();
			pagingModel = new QLBanTableModel();
			if (getSizeData() <= MAX_NUMBER_PER_PAGE) {
				getBtnNext().setEnabled(false);
				getBtnLast().setEnabled(false);
				lblPage.setText(getSizeData() + " / " + getSizeData());
				pagingModel.setRowCount(getSizeData());
			} else {
				lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
						+ getSizeData());
				pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
			}
			tableData.setModel(pagingModel);

			// sorter
			sorter = new TableRowSorter<QLBanTableModel>(pagingModel);
			tableData.setRowSorter(sorter);

			tableData.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent me) {
					if (me.getClickCount() == 2) {
						processSua();
					}
				}
			});

		}
		return tableData;
	}

	/**
	 * This method initializes panelCommand
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelCommand() {
		if (panelCommand == null) {
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(494, 1, 25, 23));
			jLabel7.setText("");
			jLabel7.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "filter.png"));
			panelCommand = new JPanel();
			panelCommand.setLayout(null);
			panelCommand.setBounds(new Rectangle(16, 26, 737, 27));
			panelCommand.add(getBtnThem(), null);
			panelCommand.add(getBtnSua(), null);
			panelCommand.add(getBtnXoa(), null);
			panelCommand.add(getTxtFilter(), null);
			panelCommand.add(jLabel7, null);
		}
		return panelCommand;
	}

	/**
	 * This method initializes btnThem
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnThem() {
		if (btnThem == null) {
			btnThem = new JButton();
			btnThem.setBounds(new Rectangle(0, 0, 90, 26));
			btnThem.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "add_new.png"));
			btnThem.setText("Thêm");
			btnThem.setContentAreaFilled(false);

			btnThem.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					processThem();
				}
			});

			btnThem.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnThem.setContentAreaFilled(true);
				}

				public void mouseExited(MouseEvent event) {
					btnThem.setContentAreaFilled(false);
				}
			});
		}
		return btnThem;
	}

	/**
	 * This method initializes btnSua
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnSua() {
		if (btnSua == null) {
			btnSua = new JButton();
			btnSua.setBounds(new Rectangle(90, 0, 85, 26));
			btnSua.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "edit.png"));
			btnSua.setText("Sửa");
			btnSua.setContentAreaFilled(false);
			final QuanLiBan qlb = this;
			btnSua.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (getTableData().getSelectedRow() > -1) {
						processSua();
					} else
						JOptionPane.showMessageDialog(qlb,
								"Bạn Hãy Chọn Bàn !", "Thông Báo",
								JOptionPane.INFORMATION_MESSAGE);
				}
			});

			btnSua.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnSua.setContentAreaFilled(true);
				}

				public void mouseExited(MouseEvent event) {
					btnSua.setContentAreaFilled(false);
				}
			});
		}
		return btnSua;
	}

	/**
	 * This method initializes btnXoa
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnXoa() {
		if (btnXoa == null) {
			btnXoa = new JButton();
			btnXoa.setBounds(new Rectangle(175, 0, 85, 26));
			btnXoa.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "delete.png"));
			btnXoa.setText("Xóa");
			btnXoa.setContentAreaFilled(false);

			btnXoa.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnXoa.setContentAreaFilled(true);
					btnXoa.setBackground(Color.getColor("#C7D3E0"));
				}

				public void mouseExited(MouseEvent event) {
					btnXoa.setContentAreaFilled(false);
				}
			});
			final QuanLiBan qlb = this;
			btnXoa.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (tableData.getSelectedRow() > -1)
						processDelete();
					else
						JOptionPane.showMessageDialog(qlb,
								"Bạn Hãy Chọn Bàn !", "Thông Báo",
								JOptionPane.INFORMATION_MESSAGE);
				}
			});
		}
		return btnXoa;
	}

	/**
	 * This method initializes txtFilter
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtFilter() {
		if (txtFilter == null) {
			txtFilter = new JTextField();
			txtFilter.setBounds(new Rectangle(519, 0, 217, 26));

			txtFilter.getDocument().addDocumentListener(new DocumentListener() {

				@Override
				public void removeUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}

				@Override
				public void insertUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}

				@Override
				public void changedUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}
			});
		}
		return txtFilter;
	}

	/**
	 * This method initializes panelChiTiet
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelChiTiet() {
		if (panelChiTiet == null) {
			panelChiTiet = new JPanel();
			panelChiTiet.setLayout(null);
			panelChiTiet.setBounds(new Rectangle(12, 297, 765, 238));
			panelChiTiet.setBorder(BorderFactory
					.createTitledBorder("Chi Tiết Bàn"));
			panelChiTiet.add(getPanelComponent(), null);
			panelChiTiet.add(getPanelImg(), null);
		}
		return panelChiTiet;
	}

	/**
	 * This method initializes panelComponent
	 * 
	 * @return javax.swing.JPanel
	 */
	private ImagePanel getPanelComponent() {
		if (panelComponent == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(0, 100, 84, 25));
			jLabel5.setText(" Mô Tả");
			jLabel5.setFont(jLabel5.getFont().deriveFont(Font.BOLD));
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(0, 75, 95, 25));
			jLabel4.setText(" Được Sử Dụng");
			jLabel4.setFont(jLabel4.getFont().deriveFont(Font.BOLD));
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(0, 50, 92, 25));
			jLabel3.setText(" Nhân Viên QL");
			jLabel3.setFont(jLabel3.getFont().deriveFont(Font.BOLD));
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(0, 25, 97, 25));
			jLabel2.setText(" Khu Vực");
			jLabel2.setFont(jLabel2.getFont().deriveFont(Font.BOLD));
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(0, 0, 94, 25));
			jLabel1.setText(" Tên Bàn");
			jLabel1.setFont(jLabel1.getFont().deriveFont(Font.BOLD));
			panelComponent = new ImagePanel(Resources_Duy.IMAGE_PATH
					+ "bgThongTinBan.png");
			panelComponent.setLayout(null);
			panelComponent.setBounds(new Rectangle(174, 30, 576, 190));
			Border line = new LineBorder(Resources_Duy.COLOR_LINE_BORDER);
			panelComponent.setBorder(line);
			panelComponent.add(jLabel1, null);
			panelComponent.add(jLabel2, null);
			panelComponent.add(getTxtTenBan(), null);
			panelComponent.add(getComboBoxKhuVuc(), null);
			panelComponent.add(jLabel3, null);
			panelComponent.add(getComboBoxNhanVien(), null);
			panelComponent.add(jLabel4, null);
			panelComponent.add(getRbDuoc(), null);
			panelComponent.add(getRbKhong(), null);
			panelComponent.add(jLabel5, null);
			panelComponent.add(getScrollPaneMota(), null);
		}
		return panelComponent;
	}

	/**
	 * This method initializes panelImg
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelImg() {
		if (panelImg == null) {
			imgBan = new JLabel();
			imgBan.setBounds(new Rectangle(10, 31, 128, 128));
			imgBan.setText("");
			imgBan.setIcon(Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT));
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(0, 0, 150, 25));
			jLabel.setText("Hình Ảnh");
			jLabel.setFont(jLabel.getFont().deriveFont(Font.BOLD));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setOpaque(true);
			jLabel.setBackground(Resources_Duy.COLOR_BG_TITLE_IMAGE);
			jLabel.setForeground(Color.WHITE);
			panelImg = new JPanel();
			panelImg.setLayout(null);
			panelImg.setBounds(new Rectangle(15, 30, 150, 190));
			Border borderLine = BorderFactory.createLineBorder(new Color(77,
					122, 153));
			panelImg.setBorder(borderLine);
			panelImg.add(jLabel, null);
			panelImg.add(getJPanel(), null);
			panelImg.add(imgBan, null);
		}
		return panelImg;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(0, 164, 150, 25));
			jPanel.setOpaque(true);
			jPanel.setBackground(new Color(77, 122, 153));
			jPanel.add(getBtnChange(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes btnChange
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChange() {
		if (btnChange == null) {
			btnChange = new JButton();
			btnChange.setBounds(new Rectangle(33, 0, 88, 25));
			btnChange.setText("Thay Đổi");
			btnChange.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					processChangeImage();
				}
			});
		}
		return btnChange;
	}

	/**
	 * This method initializes txtTenBan
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtTenBan() {
		if (txtTenBan == null) {
			txtTenBan = new JTextField();
			txtTenBan.setBounds(new Rectangle(100, 0, 189, 26));
		}
		return txtTenBan;
	}

	/**
	 * This method initializes comboBoxKhuVuc
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboBoxKhuVuc() {
		if (comboBoxKhuVuc == null) {
			comboBoxKhuVuc = new JComboBox(getAllKVB());
			comboBoxKhuVuc.setBounds(new Rectangle(100, 26, 190, 25));
		}
		return comboBoxKhuVuc;
	}

	/**
	 * This method initializes comboBoxNhanVien
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboBoxNhanVien() {
		if (comboBoxNhanVien == null) {
			comboBoxNhanVien = new JComboBox(getAllNhanVien());
			comboBoxNhanVien.setBounds(new Rectangle(100, 50, 190, 25));
		}
		return comboBoxNhanVien;
	}

	/**
	 * This method initializes rbDuoc
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRbDuoc() {
		if (rbDuoc == null) {
			rbDuoc = new JRadioButton();
			rbDuoc.setBounds(new Rectangle(100, 75, 83, 25));
			rbDuoc.setText("Được");
			groupSudung.add(rbDuoc);
		}
		return rbDuoc;
	}

	/**
	 * This method initializes rbKhong
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRbKhong() {
		if (rbKhong == null) {
			rbKhong = new JRadioButton();
			rbKhong.setBounds(new Rectangle(184, 75, 84, 25));
			rbKhong.setText("Không");
			groupSudung.add(rbKhong);
		}
		return rbKhong;
	}

	/**
	 * This method initializes scrollPaneMota
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneMota() {
		if (scrollPaneMota == null) {
			scrollPaneMota = new JScrollPane();
			scrollPaneMota.setBounds(new Rectangle(100, 100, 476, 75));
			scrollPaneMota.setViewportView(getTxtMoTa());
		}
		return scrollPaneMota;
	}

	/**
	 * This method initializes txtMoTa
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTxtMoTa() {
		if (txtMoTa == null) {
			txtMoTa = new JTextArea();
		}
		return txtMoTa;
	}

	/**
	 * This method initializes btnChapNhan
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChapNhan() {
		if (btnChapNhan == null) {
			btnChapNhan = new JButton();
			btnChapNhan.setText("Chấp Nhận");
			btnChapNhan.setBounds(new Rectangle(578, 534, 99, 25));
			btnChapNhan.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					saveObject();

				}
			});
		}
		return btnChapNhan;
	}

	/**
	 * This method initializes btnLamLai
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnLamLai() {
		if (btnLamLai == null) {
			btnLamLai = new JButton();
			btnLamLai.setText("Hủy");
			btnLamLai.setBounds(new Rectangle(676, 534, 99, 25));
			final QuanLiBan qlb = this;
			btnLamLai.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					qlb.dispose();
				}
			});
		}
		return btnLamLai;
	}

	/**
	 * This method initializes btnNext
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnNext() {
		if (btnNext == null) {
			btnNext = new JButton();
			btnNext.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "next.png"));
			btnNext.setBounds(new Rectangle(165, 1, 24, 20));
			btnNext.setContentAreaFilled(false);
			btnNext.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					indexPos = indexPos + MAX_NUMBER_PER_PAGE;
					btnNext.setEnabled(true);
					btnFirst.setEnabled(true);
					getBtnPrevious().setEnabled(true);
					if (getSizeData() - indexPos >= MAX_NUMBER_PER_PAGE) {
						pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
						pagingModel.setIndexRow(indexPos);
						if (getSizeData() - indexPos == MAX_NUMBER_PER_PAGE) {
							getBtnNext().setEnabled(false);
							getBtnLast().setEnabled(false);
						}
						lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
								+ getSizeData());
					} else {
						btnNext.setEnabled(false);
						btnLast.setEnabled(false);
						pagingModel.setIndexRow(indexPos);
						pagingModel.setRowCount(getNumberRowOdd());
						lblPage.setText(indexPos + getNumberRowOdd() + " / "
								+ getSizeData());
					}

					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();

				}
			});
		}
		return btnNext;
	}

	/**
	 * This method initializes btnPrevious
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnPrevious() {
		if (btnPrevious == null) {
			btnPrevious = new JButton();
			btnPrevious.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "previous.png"));
			btnPrevious.setContentAreaFilled(false);
			btnPrevious.setBounds(new Rectangle(86, 1, 24, 20));
			btnPrevious.setEnabled(false);
			btnPrevious.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					indexPos = indexPos - MAX_NUMBER_PER_PAGE;
					getBtnNext().setEnabled(true);
					getBtnLast().setEnabled(true);

					pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
					pagingModel.setIndexRow(indexPos);
					if (indexPos < MAX_NUMBER_PER_PAGE) {
						getBtnPrevious().setEnabled(false);
						getBtnFirst().setEnabled(false);
					} else
						getBtnPrevious().setEnabled(true);
					lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
							+ getSizeData());
					tableData.tableChanged(new TableModelEvent(tableData
							.getModel()));
				}
			});
		}
		return btnPrevious;
	}

	/**
	 * This method initializes btnFirst
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnFirst() {
		if (btnFirst == null) {
			btnFirst = new JButton();
			btnFirst.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "first.png"));
			btnFirst.setBounds(new Rectangle(54, 1, 32, 20));
			btnFirst.setContentAreaFilled(false);
			btnFirst.setEnabled(false);
			btnFirst.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					indexPos = 0;
					pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
					pagingModel.setIndexRow(indexPos);
					lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
							+ getSizeData());
					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();
					btnFirst.setEnabled(false);
					btnPrevious.setEnabled(false);
					btnNext.setEnabled(true);
					btnLast.setEnabled(true);
				}
			});
		}
		return btnFirst;
	}

	/**
	 * This method initializes btnLast
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnLast() {
		if (btnLast == null) {
			btnLast = new JButton();
			btnLast.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "last.png"));
			btnLast.setBounds(new Rectangle(189, 1, 32, 20));
			btnLast.setContentAreaFilled(false);
			btnLast.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					if (getNumberRowOdd() > 0) {
						indexPos = getSizeData() - getNumberRowOdd();
						pagingModel.setRowCount(getNumberRowOdd());
						lblPage.setText(indexPos + getNumberRowOdd() + " / "
								+ getSizeData());
					} else {
						indexPos = getSizeData() - MAX_NUMBER_PER_PAGE;
						pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
						lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
								+ getSizeData());
					}

					pagingModel.setIndexRow(indexPos);

					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();
					btnFirst.setEnabled(true);
					btnPrevious.setEnabled(true);
					btnLast.setEnabled(false);
					btnNext.setEnabled(false);
				}
			});
		}
		return btnLast;
	}

	/**
	 * This method initializes panelPaging
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelPaging() {
		if (panelPaging == null) {
			panelPaging = new JPanel();
			panelPaging.setLayout(null);
			panelPaging.setBounds(new Rectangle(534, 238, 222, 22));
			panelPaging.add(getBtnFirst(), null);
			panelPaging.add(getBtnPrevious(), null);
			panelPaging.add(lblPage, null);
			panelPaging.add(getBtnNext(), null);
			panelPaging.add(getBtnLast(), null);
		}
		return panelPaging;
	}

}
